document.onkeyup = __autoComboBoxTextChanged;

var __comboBoxVal; // the main TextBox
var __comboBoxLastVal;// a hidden form field ,use to save last value
var __comboBoxIdVal;//
var __comboBoxSelectedSuggestion = 0; // the current sequence number
var __comboBoxSuggestions;// all Suggestions
var __comboBoxHasFocus;// the main TextBox is whether focus or not
var __comboBoxCssClass;
var __comboBoxSelectedCssClass;
var __comboBoxWaitImage;
var resultList = null;// all Suggestions got from server , is a javascript array

var floaterIframe;

function __comboBoxCallback(result, context) {
    var r = document.getElementById(context);
    
    __comboBoxWaitImage.style.visibility = 'hidden';
    
    resultList = eval(result);
    if( resultList.length < 1 )
    {
        floaterIframe.style.display = 'none';
        r.style.display = 'none';
        return;
    }
    floaterIframe.style.display = 'block';
    
    r.style.display = 'block';
    r.innerHTML = '';
    for (var i = 0; i < resultList.length; i += 2)
    {
		var d = document.createElement('div');
		// What happens if the callback happens after a diff. comboBox was selected?
		if (i == 0)
			d.className = __comboBoxSelectedCssClass;
		else
			d.className = __comboBoxCssClass;
		d.onmousedown = function() { __comboBoxSelect(this); r.innerHTML = ''; r.style.display = 'none'; floaterIframe.style.display = 'none';};
		d.onmouseover = function() { __comboBoxMouse(this, true); }
		d.onmouseout = function() { __comboBoxMouse(this, false); }
		d.innerHTML = resultList[i];
		d.title = resultList[i + 1];// 
		
		r.appendChild(d);
    }
    
    var rct = r.getBoundingClientRect();
    var width1 = rct.right - rct.left;
    if(width1 > 180) width1 = 180;
    floaterIframe.style.width  = rct.right - rct.left;//"180px"
    floaterIframe.style.height = rct.bottom - rct.top;//"180px";//rct.bottom - rct.top
    //if(floaterIframe.style.width > 100) alert(floaterIframe.style.width);
    
//    var height1 = rct.bottom - rct.top;
//    var dd = document.createElement('div');
//    dd.className = __comboBoxCssClass;
//    dd.innerHTML = floaterIframe.style.width.toString() + height1.toString();
//    r.appendChild(dd);

    if (floaterIframe.contentDocument && floaterIframe.contentDocument.documentElement.offsetHeight)
    {
        floaterIframe.height = floaterIframe.contentDocument.documentElement.offsetHeight+"px";
    }
    else if(floaterIframe.document && floaterIframe.contentWindow.document.documentElement)
    {
        floaterIframe.height = floaterIframe.contentWindow.document.documentElement.scrollHeight+"px";
    }

    
    __autoComboBoxUpdateSelection(0, false);
}

function __comboBoxMouse(el, b)
{
    el.className = b ? __comboBoxSelectedCssClass : __comboBoxCssClass;
}

function __autoTextBoxShouldRefresh()
{
	var sr = (__comboBoxLastVal.value != __comboBoxVal.value);
	if (sr)
	{
	    __comboBoxWaitImage.style.visibility = 'visible';
	}
	__comboBoxLastVal.value = __comboBoxVal.value;
	return sr;
}
//function __comboBoxTextChanged(e)
function __autoComboBoxTextChanged(e)
{
    //alert("__comboBoxTextChanged");
	if (__comboBoxHasFocus)
	{
		if (!e)
			e = event;

		if (e.keyCode == 38) // Up.
		{
			__autoComboBoxSelectPreviousSuggestion();
		}
		else if (e.keyCode == 40) // Down.
		{
			__autoComboBoxSelectNextSuggestion();
		}
	}
}

function __autoComboBoxSelectNextSuggestion()
{
	var newSelectedSuggestion;
	if (__comboBoxSelectedSuggestion < (__comboBoxSuggestions.length - 1))
	{
		newSelectedSuggestion = __comboBoxSelectedSuggestion + 1;
		__autoComboBoxUpdateSelection(newSelectedSuggestion, true);
	}
}

function __autoComboBoxUpdateSelection(newSelectedSuggestion, updateInput)
{
    //alert("__comboBoxUpdateSelection");
	// Reset the current selection.
	if (resultList.length < 1) return; // 2006-7-4
	
	__comboBoxSuggestions[__comboBoxSelectedSuggestion].className = __comboBoxCssClass;

	// Set the new selection.
	__comboBoxSelectedSuggestion = newSelectedSuggestion;
	
	// 
	if (resultList.length > 0)
	{
	    //__comboBoxVal.title = resultList[__comboBoxSelectedSuggestion + 1];
	    __comboBoxIdVal.value = resultList[__comboBoxSelectedSuggestion + 1];
	}
		
	var i = __comboBoxSuggestions[__comboBoxSelectedSuggestion];
	i.className = __comboBoxSelectedCssClass;

	if (updateInput)
	{
		__comboBoxVal.value = __comboBoxLastVal.value = i.innerHTML;
		
		var l = __comboBoxVal.value.length;

		if (__comboBoxVal.setSelectionRange)
		{
			__comboBoxVal.setSelectionRange(l, l);
		}
		else if (__comboBoxVal.createTextRange)
		{
			var tr = __comboBoxVal.createTextRange();
			tr.moveStart('character', l);
			tr.select();
		}
	}
}

function __autoComboBoxSelectPreviousSuggestion()
{
	var newSelectedSuggestion;
	if (__comboBoxSelectedSuggestion > 0)
	{
		newSelectedSuggestion = __comboBoxSelectedSuggestion - 1;
		__autoComboBoxUpdateSelection(newSelectedSuggestion, true);
	}
}

function __comboBoxSelect(item)
{
	__comboBoxVal.value = item.innerHTML;
	__comboBoxVal.focus();
	 
	if (resultList.length > 0)
	{
	    //__comboBoxVal.title = item.title;
	    __comboBoxIdVal.value = item.title;
	    
	}
	
	// Simulate enter.
	if (document.createEventObject)
	{
		var eo = document.createEventObject();
		eo.keyCode = 13;
		__comboBoxVal.fireEvent("onkeypress", eo);
	}
}

function __comboBoxUpdateFocus(b, val, lastVal, idValue, results, iframeId, img, css1, css2)
{
    //alert("__comboBoxUpdateFocus");
    // b: whether show result items or not
	// Reset the selection of the currently shown results.
	if (!b && __comboBoxSuggestions && __comboBoxSuggestions.length > 0)
		__comboBoxSuggestions[__comboBoxSelectedSuggestion].className = __comboBoxCssClass;
    
    __comboBoxWaitImage = document.getElementById(img);
	__comboBoxVal = document.getElementById(val);
	__comboBoxLastVal = document.getElementById(lastVal);
	__comboBoxIdVal = document.getElementById(idValue);
	
	floaterIframe = document.getElementById(iframeId);//
	var r = document.getElementById(results);
	
	if(__comboBoxVal.value == "")
	{
	    floaterIframe.style.display = 'none';
	    r.style.display = 'none';
	}
	
	if (!b)
	{
	    __comboBoxWaitImage.style.visibility = 'hidden';
	    floaterIframe.style.display = 'none';
		r.style.display = 'none';
		r.innerHTML = '';
	}
	
	var topOffset = 16;
	if (__comboBoxVal.rows)
		topOffset *= __comboBoxVal.rows;
	
	var width;
	if (__comboBoxVal.width)
		width = __comboBoxVal.width;
	else if (__comboBoxVal.style.width)
		width = __comboBoxVal.stylewidth;
	
	r.style.top = __comboBoxGetRealTop(__comboBoxVal) + topOffset + 3;//topOffset + 6
	r.style.left = __comboBoxGetRealLeft(__comboBoxVal);
//	if (width)
//	{
//		r.style.width = width + 'px';
//	}
//	
//	floaterIframe.style.top = r.style.top;
//	floaterIframe.style.left = r.style.left;//offsetHeight
	//floaterIframe.style.height = r.offsetHeight;
	//floaterIframe.style.width = "130px"; //r.offsetWidth;
	
	if (width)
	{
	    r.style.width = width + 'px';
	}
	else
	{
	    //r.style.width = '180px';
	    //floaterIframe.style.width = '182px';
	}
	__comboBoxWaitImage.style.top = __comboBoxGetRealTop(__comboBoxVal);
	__comboBoxWaitImage.style.left = __comboBoxGetRealLeft(__comboBoxVal) - 18;
		
	floaterIframe.style.top = r.style.top;
	floaterIframe.style.left = r.style.left;
	//alert(floaterIframe.style.width);
	
	__comboBoxSuggestions = r.getElementsByTagName('div');
	__comboBoxSelectedSuggestion = 0;
	__comboBoxCssClass = css1;
	__comboBoxSelectedCssClass = css2;


	
	__comboBoxSuggestions = r.getElementsByTagName('div');
	__comboBoxSelectedSuggestion = 0;
	__comboBoxCssClass = css1;
	__comboBoxSelectedCssClass = css2;

	__comboBoxHasFocus = b;
}

function __comboBoxLoseFocus(results, iframeId)
{
    floaterIframe = document.getElementById(iframeId);//
	var r = document.getElementById(results);
	
	floaterIframe.style.display = 'none';
	r.style.display = 'none';
	r.innerHTML = '';
}

function __comboBoxGetRealTop(el) {
	yPos = el.offsetTop;
	tempEl = el.offsetParent;
	while (tempEl != null)
	{
		yPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}
	return yPos;
}

function __comboBoxGetRealLeft(el) {
	xPos = el.offsetLeft;
	tempEl = el.offsetParent;
	while (tempEl != null)
	{
		xPos += tempEl.offsetLeft;
		tempEl = tempEl.offsetParent;
	}
	return xPos;
}